const Sequelize = require('sequelize')
const {sequelize} = require('@/mysql/utils')
const {Project} = require('@/mysql/project')
const {User} = require('@/mysql/user')

// 创建用户实体
const Note = sequelize.define('nonte', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: Sequelize.STRING,
            allowNull: false
        },
        description: {
            type: Sequelize.TEXT
        },
        create_user: {
            type: Sequelize.STRING
        },
        deleted: {
            type: Sequelize.BOOLEAN,
            defaultValue: false
        }
    }
);
Note.belongsTo(Project)
Note.belongsTo(User)
Note.sync({force: false})
/**插入新分享类型数据（添加） */
Note.insertData = async (name, description, project_id) => {
    await Note.sync({force: false})
    // 表已创建
    return Note.create({
        name, description, projectProjectId: project_id, userId: global.utils.getUserId()
    })
}

/** 更新分享数据 */
Note.updateData = async (id, name, description) => {
    return Note.update({
        name, description
    }, {
        where: {id: id}
    })
}
/**
 * @functionName: 查找留言
 * @param:  Note
 * @date 2019/5/19 10:29
 * @description 获取留言
 * @version 1.0
 */
Note.getAllNote = async (id, deleted = false) => {
    return Note.findAll({
        where: {projectProjectId: id, deleted}, include: [{
            model: User,
            where: {id: Sequelize.col('userId')}
        }]
    })
}
export {Note}

